<?php
require_once (dirname(__FILE__) .
"/../config/config.php");
include 'user.php';
?>
<?php

//&first_name=XXXXXX&last_name=XXXX&password=123456&email=test@gmail.com&mobile=0430000001
//获取method 判断是注册 还是分享

/**
 * 成功提示代码从99递减,错误提示代码从00递增.代码四位,前两位表示API ID,后两位用来描述提示信息.以后都按此标准走.
 * 代码0001,无返回结果
 * 代码0100,没有method参数
 * 代码0199,请求执行成功
 * 代码0101,用户名已存在
 * 代码0102,邮箱已存在
 * 代码0103,first_name不能为空
 * 代码0104,last_name不能为空
 * 代码0105,手机号已存在
 * 代码0106,手机不能为空
 * 代码0107,邮箱不能为空
 * 代码0108,密码不能为空
 * 代码0109,邮箱已存在
 * 代码0110,CP创建用户失败,SQL执行错误
 * 代码0111,CP分配服务失败.
 * 代码0112,注册验证码校验失败
 * 代码0113,注册验证码过期失效
 * 代码0198,注册验证码校验成功
 * 代码0114,验证码不能为空
 * 代码0115,验证码内容不能为空
 * 代码0116,用户不存在
 *
 *
 * 代码0299,分享成功
 * 代码0201,缺少owner参数.
 * 代码0202,owner不存在.
 *
 * 代码0499,手机发送成功
 * 代码0498,邮件发送成功
 * 代码0496  修改密码成功
 * 代码0495  用户修改信息成功
 * 代码0494  第三方登陆信息绑定成功
 * 
 * 代码0400 不存在该手机用户
 * 代码0401  不存在该邮箱
 * 代码0402  验证信息和验证码不匹配
 * 代码0404  用户修改密码超出有效期  不允许修改密码
 * 0405 netcube web 登录错误
 * 0406 netcube web 登录错误
 * 0407 netcube web 登录错误

 * */

if (isset ($_REQUEST['method'])) {
	/*新用户注册*/
	//发送验证码
	if ($_REQUEST['method'] == "register_send_code") {
		$result = register_send_code();
	}
	//验证码检查
	elseif ($_REQUEST['method'] == "checkVerifiCode") {
		$result = checkVerifiCode($_REQUEST['code'], $_REQUEST['check_content']);
	}
	//注册
	elseif ($_REQUEST['method'] == "register") {
		$result = register_user();
	}
	//用户基本信息修改
	elseif ($_REQUEST['method'] == "update_user") {
		$result = update_user();
	}
	//显示用户基本信息
	elseif($_REQUEST['method']=='show_user')
	{
		$result=show_user();
	}
	//显示客户开通的服务
	elseif ($_REQUEST['method']=='show_service')
	{
		$result=show_service();
	}
	//第三方登陆信息绑定
	elseif ($_REQUEST['method'] == "bind_information") {
		$result = bind_information();
	}
	//分享列表
	elseif ($_REQUEST['method'] == "sharelist") {
			$result = doShare();
		} 
		elseif ($_REQUEST['method'] == "checkVerifiCode") {
				$result = checkVerifiCode($_REQUEST['code'], $_REQUEST['check_content']);
			}
	//发送验证码
	elseif ($_REQUEST['method'] == "sendCode") {
			$result = sendCode();
		}
	//判断验证码是否与数据库了存入的一致
	elseif ($_REQUEST['method'] == "checkCode") {
			$result = checkCode();
		}
	//判断验证码是否与数据库了存入的一致
	elseif ($_REQUEST['method'] == "checkPassword") {
			$result = checkPassword();
		}
	//修改密码
	elseif ($_REQUEST['method'] == "setPassword") {
			$result = setPassword();
		}
	elseif ($_REQUEST['method'] == "login_by_third") {
		$result = login_by_third();
	}
	elseif ($_REQUEST['method'] == "showuseridBymacaddress") {
		$result = showuseridBymacaddress($_REQUEST['mac_address']);
	}
	//分享列表
	elseif ($_REQUEST['method'] == "showuseridByUsernameAndPassword") {
			$result = showuseridByUsernameAndPassword($_REQUEST['user_name'], $_REQUEST['password']);
	}elseif($_REQUEST['method']=="loginByEmail"){
		$result=loginByEmail();
	}elseif ($_REQUEST['method']=="customerByEmail"){
		$result=customerByEmail();
	}elseif ($_REQUEST['method']=="agentByEmail"){
		$result=agentByEmail();
	}elseif($_REQUEST['method']=="AgentloginByEmail")
	{
		$result=AgentloginByEmail();
	}else if ($_REQUEST['method']=="updatePassword"){
		$result=updatePassword();
	}
	output_result("json", $result);
} else {
	output_result();
	exit ();
}

function doShare() {
	//appuser.php?method=sharelist&owner=cpusername&contactlist=Name1|0430000001,Name2|0430000002
	//获取参数
	$owner = $_REQUEST['owner']; //分享人
	if (!isset ($_REQUEST['owner'])) {
		return array (
			"message" => "0201"
		);
	}
	/** 判断分享人是否存在系统 **/
	$sql = "select * from zy_user where user_name='$owner'";
	$rs = mysql_query($sql);
	$row = mysql_fetch_array($rs);
	if (!$row) {
		return array (
			"message" => "0202"
		);
	}
	$owner_id = $row['user_id'];
	$invite_code = $row['invite_code'];
	$contactlist = $_REQUEST['contactlist']; //被分享人列表,获取到的数据形式为被分享人|(电话号码)0430000001,Name2|0430000002
	//设置参数
	$contacts = explode(",", $contactlist);
	$share_time = date("Y-m-d H:i:m");
	$j = 0; //分享成功后,累加J的值统一提示分享成功
	for ($i = 0; $i < count($contacts); $i++) {
		$contact = explode("|", $contacts[$i]);
		$sql = "INSERT INTO `zy_share_list`(`owner_id`,`shared_person`,`shared_phone`,`share_time`) VALUES ('$owner_id','$contact[0]','$contact[1]','$share_time')";
		$rs = mysql_query($sql);
		$share_id = mysql_insert_id(); //返回自增的user_id;
		$url = "http://cp.netcube.com.au/api/appuser.php?s=" . base64_encode($owner_id);
		$text = $row['first_name'] . '分享了一款APP：NetCube,您的邀请码为:' . $invite_code . '下载后可立即获得2元话费,APP下载地址为:' . $url;
		sendSms($contact[1], $text); //发送短信
	}
	return array (
		"message" => "0299"
	);
}
//发送短信
function sendSms($moble, $text) {
	$post_data = array (
		'method' => sendMessages,
		'username' => abc,
		'password' => 123456,
		'mobile' => $moble,
		'api_id' => 53,
		'text' => $text
	);
	//这里做短信发送,发送短信的时候,要从zy_gift_password 中 抽取一个 随机密码,发送到客户手机,还有领奖地址
	$url = "http://sms.webnova.com.au/api/webnova_sms_api_lib.php";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
	curl_setopt($ch, CURLOPT_POST, 1);
	// 把post的变量加上
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	$output = curl_exec($ch);

	//$aa=array('message'=>$output);
	//echo json_encode($aa);

	//调试使用
	if ($output === FALSE) {
		$output = "cURL Error: " . curl_error($ch);

	}
	curl_close($ch);
}

function output_result($type = "json", $result = array ()) {
	if ($type == "json") {
		if (empty ($result)) {
			$message = array (
				"message" => "0001"
			);
		} else {
			$message = $result;
		}
		echo json_encode($message);
		exit ();
	}
}
function register_user() {
	$mobile = isset ($_REQUEST['mobile']) ? $_REQUEST['mobile'] : $message = "0108";
	$code = $_REQUEST['code']; //验证码
	if (isset ($message)) {
		return array (
			"message" => $message
		);
	}
	//判断下是否曾经被邀请过,如果有的话记录下邀请人ID
	if (isset ($_REQUEST['invite_code'])) {
		$owner_id = checkInviteCode($_REQUEST['invite_code'], $_REQUEST['mobile']); //用户输入的邀请码是否存在    存在就将该邀请码所有者的user_id获取
	}

	//如果用户使用第三方注册
	if (isset ($_REQUEST['third_api_id'])) {
		$third_api_id = $_REQUEST['third_api_id']; //第三方媒体id
		$way = $_REQUEST['way']; //第三方登陆方式   weibo   facebook  twiter
		$user = array ();
		$user['third_api_id'] = $third_api_id;
		$user['way'] = $way;
		$user['first_name'] = $_REQUEST['first_name'];
		$user['last_name'] = $_REQUEST['last_name'];
		$user['password'] = $_REQUEST['password'];
		$user['email'] = $_REQUEST['email'];
		$user['mobile'] = $mobile;
		$user['invite_code'] = create_invite_code(); //生成invite_code	
		$user['invite_user_id'] = $owner_id;
		$result = register_user_php($user, $_REQUEST['user_name_type']);
		//如果有验证码,则需要进行验证.否则不验证.
		if (isset ($_REQUEST['code'])) {
			$result = checkVerifiCode($code, $mobile); //如果验证码正确返回值为2
			if ($result['message'] != "0198") //验证码不正确
				{
				return $result;
			}
		}
		if ($result['message'] == '0397') {
			//存在该用户的第三方登陆的记录  返回该用户的user_id
			return array("message"=>'0397','user_id'=>$result['user_id']);
			//$user_id = $result['user_id'];
		}
		elseif ($result['message'] == '0398') {
			return array (
				"message" => '0398',
				'user_id' => $result['user_id']
			);
		} else {
			return array (
				"message" => $result['message']
			);
		}

	} else {
		$first_name = isset ($_REQUEST['first_name']) ? $_REQUEST['first_name'] : $message = "0103";
		$last_name = isset ($_REQUEST['last_name']) ? $_REQUEST['last_name'] : $message = "0104";
		$password = isset ($_REQUEST['password']) ? $_REQUEST['password'] : $message = "0105";
		$email = isset ($_REQUEST['email']) ? $_REQUEST['email'] : $message = "0107";
		$mobile = isset ($_REQUEST['mobile']) ? $_REQUEST['mobile'] : $message = "0108";
		$invite_code = $_REQUEST['invite_code']; //邀请码
		$code = $_REQUEST['code']; //验证码
		if (isset ($message)) {
			return array (
				"message" => $message
			);
		}
		//判断数据是否重复
		/*判断用户名是否存在*/
		$username = explode("@", $email);
		$sql = "select user_name from zy_user where user_name='" . $username[0] . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0101"
			);
		}
		/*判断手机号是否存在*/
		$sql = "select telephone from zy_user where telephone='" . $mobile . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0105"
			);
		}
		/*判断邮箱是否存在*/
		$sql = "select email from zy_user where email='" . $email . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0109"
			);
		}
		//如果有验证码,则需要进行验证.否则不验证.
		if (isset ($_REQUEST['code'])) {
			$result = checkVerifiCode($code, $mobile); //如果验证码正确返回值为2
			if ($result['message'] != "0198") //验证码不正确
				{
				return $result;
			}
		}

		$own_invite_code = create_invite_code();
		$sql = "INSERT INTO `zy_user`(`user_name`,`password`,`level`,`first_name`,`last_name`,`telephone`,`email`,`invite_code`,`invite_user_id`,reg_time) VALUES ('$username[0]','$password',2,'$first_name','$last_name','$mobile','$email','$own_invite_code',$owner_id,now())";
		$rs = mysql_query($sql);
		if (!$rs) {
			return array (
				"message" => "0110"
			);
		}
		$user_id = mysql_insert_id(); //返回自增的user_id;
		//注册成功后绑定服务
		//设置绑定服务参数
	}
	//分配服务
	$valid_date = date("Y-m-d", strtotime("+1 year"));
	$user_service_id = distribution_service($user_id, $service_id = 3, $valid_date);
	if (!$user_service_id > 0) {
		return array (
			"message" => "0111"
		);
	}

	//创建a2billing账号及密码.
	$post_data = array (
		'first_name' => $first_name,
		'last_name' => $last_name,
		'email' => $email,
		'mobile' => $mobile,
		'method' => "addAccount"
	);
	$url = "http://billing3.novatel.com.au/allen/app_user_api.php";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	$output = curl_exec($ch);
	if ($output === FALSE) {
		$output = "cURL Error: " . curl_error($ch);
	} else {
		$result = json_decode($output);
		//绑定a2b的帐号密码.
		create_login_list($user_service_id, $result->account_number, $result->uipass, $result->id);
	}
	curl_close($ch);
	//给邀请人增加2元话费.
	if ($owner_id > 0) {
		$sql = "SELECT c.* FROM zy_user AS a " .
		"LEFT JOIN zy_user_service AS b ON a.`user_id` =b.`user_id` " .
		"LEFT JOIN zy_login_list AS c ON b.`user_service_id`=c.`user_service_id` 
						WHERE a.`user_id`='$owner_id' AND b.`service_id`=3 ";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			$a2b_username = $row['field1'];
			$description_refill = "share to $username[0] success.";
		}
		$post_data = array (
			'balance' => 2,
			'username' => $a2b_username,
			'description_refill' => $description_refill,
			'method' => "refill"
		);
		$url = "http://billing3.novatel.com.au/allen/app_user_api.php";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		curl_close($ch);
	}
	return array (
		"message" => "0199"
	);
}

//分配设备
function distribution_service($user_id, $service_id, $valid_date) {
	//分配服务/绑定a2billing账号密码.
	$sql = "INSERT INTO `zy_user_service`(`user_id`,`service_id`,`valid_date`,`status`,`create_user`,`access_id`) VALUES ('$user_id',$service_id,'$valid_date','1','1','$user_id')";
	$rs = mysql_query($sql);
	$user_service_id = mysql_insert_id(); //返回自增的user_id;
	return $user_service_id;
}
function create_login_list($user_service_id, $a2b_account, $a2b_password, $a2b_account_id) {
	$sql = "insert into zy_login_list(user_service_id,field1,field2,field3,field4,field5,field6) values($user_service_id,'$a2b_account','$a2b_password','billing3.novatel.com.au','1','2','$a2b_account_id')";
	$rs = mysql_query($sql);
	return $rs;
}
//软件下载
function download_app() {
	$str = $_REQUEST['s'];
	$share_id = base64_encode($str);
	$sql = "update zy_share_list SET shared_status=1 and download_time='" . date("Y-m-d H:i:m") . "' where share_id=" . $share_id;
	$download_url = "https://itunes.apple.com/au/app/netcube/id595091647?mt=8";
	header("location:" . $download_url);
}

//发送验证码
function sendCode() {
	/*if(isset($_REQUEST['email'])){
	
	}
	if(isset($_REQUEST['mobile'])){
	
	}
	$information=$_REQUEST['information'];
	if(indexOf("@")>1)*/
	$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
	for ($i = 0; $i < 6; $i++) {
		$invite_code .= $pattern {
			mt_rand(0, 9)
			};
	}
	if (isset ($_REQUEST['mobile'])) {
		//$my='13215558580';
		$mobile = $_REQUEST['mobile'];
		$sql = "select * from zy_user where telephone='$mobile'";

		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			$user_id = $row['user_id'];
			$time = date('Y-m-d H:i:s');
			$sql1 = " insert into `zy_verification`(`user_id`,`check_content`,`code`,`valid_time`,`start_time`) values ($user_id,'$mobile','$invite_code',60,'$time')";
			$rs = mysql_query($sql1);
			$content = "Your NetCube Password Reset Verification Code is $invite_code";
			sendSms($mobile, $content);
			return array (
				'message' => '0499'
			); //发送短信成功
		} else {
			return array (
				'message' => '0400'
			);
		}
	}
	if (isset ($_REQUEST['email'])) {
		//$my1='1106142361@qq.com';
		$email = $_REQUEST['email'];
		$sql = "select * from zy_user where email='" . $email . "'";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			$user_id = $row['user_id'];
			$time = date('Y-m-d H:i:s');
			$sql1 = " insert into `zy_verification`(`user_id`,`check_content`,`code`,`valid_time`,`start_time`) values ($user_id,'$email','$invite_code',60,'$time')";
			$rs = mysql_query($sql1);
			//如果存在email，就给email发送邮件
			$subject = "NetCube | Password Reset";
			$message = "
						Dear " . $row['user_name'] . ",
			
						Please find below your Verification Code for NetCube Password Reset as requested.
						
						--------------------------------------------------------------
						Your Verification Code is  $invite_code.
						--------------------------------------------------------------
						
						Please enter it in our NetCube App or Website to finished the password reset process.
						
						If you have any questions or need assistance with your account management, please contact us.
						
						Thank you for choosing NetCube.
						
						Best Regards,
						
						NetCube Support Team
						
						e: support@netcube.com.au
						w: www.netcube.com.au ";
			$from = "noreply@netcube.com.au";
			$headers = "From: $from";
			mail($email, $subject, $message, $headers);
			return array (
				"message" => "0498"
			);

		} else {
			return array (
				"message" => "0401"
			);
			//$message=array('message'=>'0398');
			//echo json_encode($message);

			//不存在该email
		}

	}

}

function setPassword() //修改密码时将刚才的输入的邮箱或密码  验证码的值带过来
{

	$code = $_REQUEST['code'];
	$password = urldecode($_REQUEST['password']);
	if (isset ($_REQUEST['mobile'])) { //用户输入的是mobile
		$infomation = $_REQUEST['mobile'];
		$type = "telephone";
	} else
		if (isset ($_REQUEST['email'])) {
			$infomation = $_REQUEST['email'];
			$type = "email";
		}
	$sql = "select * from zy_verification where check_content='$infomation' and code='$code' and is_used=0";
	if ($rs = mysql_query($sql)) {
		$row = mysql_fetch_array($rs);
		$querycode = $row['code'];
		$queryvalidtime = $row['valid_time'];
		$querystarttime = $row['start_time'];
		//根据手机号查询日期，如果超过有效期就不让用户修改密码
		$date_min = date("Y-m-d H:i:s", mktime(date("H"), date("i") - $queryvalidtime, date("s") - 10, date("m"), date("d"), date("Y")));
		if ($date_min > $querystarttime) //如果值大于创建的时间  则不给修改  超出有效期
			{
			return array (
				'message' => '0404'
			);
		} else { //未超出有效期
			if ($code == $querycode) {
				$sql = "update zy_user SET password='$password'  where $type='$infomation'";
				$rs = mysql_query($sql);
				$sql = "update zy_verification SET is_used=1,update_time='" . date('Y-m-d H:i:s') . "' where id=" . $row['id'];
				$rs = mysql_query($sql);
				return array (
					'message' => '0497'
				);
				//$message=array('message'=>'修改密码成功');//两次输入的验证码一致
				//echo json_encode($message);
			}
		}
	} else {
		return array (
			'message' => '0402'
		);
	}
}

function checkVerifiCode($code, $check_content) {
	//根据验证方式(手机,邮箱),验证用户输入的验证码是否正确.
	//1.验证码不正确
	//2.验证正确,但已过期
	//3.验证码正确.
	if ($code == "") {
		return array (
			'message' => '0114'
		); //验证码不能为空
	}
	if ($check_content == "") {
		return array (
			'message' => '0115'
		); //验证码类型不能为空
	}
	$sql = "select * from zy_verification where check_content='$check_content' and code='$code' and is_used=0";
	//echo $sql;
	$rs = mysql_query($sql);
	if ($row = mysql_fetch_array($rs)) {
		$querycode = $row['code'];
		$queryvalidtime = $row['valid_time'];
		$querystarttime = $row['start_time'];
		//根据手机号查询日期，如果超过有效期就不让用户修改密码
		$date_min = date("Y-m-d H:i:s", mktime(date("H"), date("i") - $queryvalidtime, date("s") - 10, date("m"), date("d"), date("Y")));
		if ($date_min > $querystarttime) //验证正确,但已过期
			{
			return array (
				'message' => '0113'
			); //注册验证码过期失效
		} else { //未超出有效期
			if ($code == $querycode) { //并且验证码是正确的
				return array (
					'message' => '0198'
				); //注册验证码校验成功
			}
		}
	} else {
		return array (
			'message' => '0112'
		); //注册验证码校验失败
	}

}
function checkInviteCode($invite_code, $mobile) {
	//1.先检查邀请码是否存在,如果存在获取邀请人user_id
	//2.根据邀请人user_id检查是否有邀请记录.
	//判断下是否曾经被邀请过,如果有的话记录下邀请人ID
	$owner_id = 0;
	$sql = "select * from zy_user where invite_code='$invite_code'";
	$rs = mysql_query($sql);
	if ($row = mysql_fetch_array($rs)) {
		$sql = "select owner_id from zy_share_list where shared_phone='" . $mobile . "'";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			$owner_id = $row['owner_id'];
		}
	}
	return $owner_id;

}

//当用户输入手机号的时候给zy_verification表添加一笔信息
function register_send_code() {
	$mobile = $_REQUEST['mobile'];
	/*判断手机号是否存在*/
		$sql = "select telephone from zy_user where telephone='" . $mobile . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0105"
			);
		}
	$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
	for ($i = 0; $i < 6; $i++) {
		$invite_code .= $pattern {
			mt_rand(0, 9)
			};
	}
	$time = date('Y-m-d H:i:s');
	$sql = " insert into `zy_verification`(`check_content`,`code`,`valid_time`,`start_time`) values ('$mobile','$invite_code',60,'$time')";
	$rs = mysql_query($sql);
	$content = "Your NetCube Register Verification Code is $invite_code";
	sendSms($mobile, $content);
	return array (
		'message' => '0499'
	);

}

//根据用户user_id修改用户的信息
function update_user()
{
	$user_id=$_REQUEST['user_id'];
	$sql="select * from zy_user where user_id=$user_id";
	$rs=mysql_query($sql);
	$row=mysql_fetch_array($rs);
	$user_name = isset ($_REQUEST['user_name']) ? $_REQUEST['user_name'] : $row['user_name'];
	$password = isset ($_REQUEST['password']) ? $_REQUEST['password'] : $row['password'];
	$first_name = isset ($_REQUEST['first_name']) ? $_REQUEST['first_name'] :$row['first_name'];
	$last_name = isset ($_REQUEST['last_name']) ? $_REQUEST['last_name'] : $row['last_name'];	
	$email = isset ($_REQUEST['email']) ? $_REQUEST['email'] :$row['email'];
	$mobile = isset ($_REQUEST['mobile']) ? $_REQUEST['mobile'] :$row['mobile'];	
	$sql="update `zy_user` set `user_name`='$user_name',`password`='$password',`first_name`='$first_name',`last_name`='$last_name',`telephone`='$mobile',`email`='$email' where `user_id`='$user_id'";
	mysql_query($sql);
	return array (
		'message' => '0495'
	);
}

//显示用户信息
function show_user()
{
	$user_id=$_REQUEST['user_id'];
	$sql='select * from zy_user where user_id='.$user_id;
	$rs=mysql_query($sql);
	if($row=mysql_fetch_array($rs)){
	    $user=array(
	            "user_id"=>$row['user_id'],
	            "user_name"=>$row['user_name'],
	            "email"=>$row['email'],
	            "first_name"=>$row['first_name'],
	            "last_name"=>$row['last_name'],
	            "invite_user_id"=>$row['invite_user_id'],
	            "reg_time"=>$row['reg_time']
	    );
	    return $user;
	}else{
	    return array (
	            'message' => '0116'
	    );
	}
}

//显示客户开通的服务
function show_service()
{
    $user_id=$_REQUEST['user_id'];
    $sql='select a.service_id,a.code,(select max(b.user_service_id) from zy_user_service b where b.service_id=a.service_id and b.user_id=2)as user_service_id from zy_service a';
    $rs=mysql_query($sql);
    while($row=mysql_fetch_array($rs)){
        $a[]=array(
        	"service_id"=>$row['service_id'],
                
            "service_code"=>$row['code'],
            "switch"=>$row['user_service_id']>0?1:0
        );
    }
    return $a;
}

//第三方登陆信息绑定
function  bind_information()
{
	$user_id=$_REQUEST['user_id'];
	$third_api_id = $_REQUEST['third_api_id']; //第三方媒体id
	$way = $_REQUEST['way']; //第三方登陆方式   weibo   facebook  twiter
	$time = date('Y-m-d H:i:s');
	$sql="insert into `zy_third_api`(`user_id`,`third_api_id`,`type`,`create_time`) values ($user_id,'$third_api_id','$way','$time')";
	mysql_query($sql);
	return array (
		'message' => '0494'
	);
}

function  loginByEmail()
{
	$sql="select * from zy_user where email='".$_REQUEST['email']."' and password='".$_REQUEST['password']."'";
	$rs=mysql_query($sql);

	if($row=mysql_fetch_array($rs))
	{
		$message=array(
				'user_id'=>$row['user_id'],
				'user_name'=>$row['user_id'],
				'password'=>$row['password'],
				'parentId'=>$row['parentId'],
				'level'=>$row['level'],
				'first_name'=>$row['first_name'],
				'last_name'=>$row['last_name'],
				'telephone'=>$row['telephone'],
				'email'=>$row['email'],
				'invite_code'=>$row['invite_code'],
				'invite_user_id'=>$row['invite_user_id'],
				'reg_time'=>$row['reg_time'],
				'isdelete'=>$row['isdelete'],
				'iscustomer'=>$row['iscustomer'],
		);
		
	}else{
	 $message=array('message'=>'0405');
	}
	
return $message;
}

/*netcube web 找回密码*/
function customerByEmail(){
	$email = $_REQUEST['email'];
	$sql="select password from zy_user where email='".$email."'";
	$rs = mysql_query($sql);
	$row = mysql_fetch_array($rs);
	if($row != false){
		$message=array('password'=>$row['password']);
	}else{
		$message=array('message'=>'0407');//错误,未找到密码
	}
	return $message;
}

/*netcube web 找回密码*/
function agentByEmail(){
	$email = $_REQUEST['email'];
	$sql="select password from zy_agent_user where agent_email='".$email."'";
	$rs = mysql_query($sql);
	$row = mysql_fetch_array($rs);
	if($row != false){
		$message=array('password'=>$row['password']);
	}else{
		$message=array('message'=>'0408');//错误,未找到密码
	}
	return $message;
}


function  AgentloginByEmail()
{
	$sql="select * from zy_agent_user where agent_email='".$_REQUEST['agent_email']."' and password='".$_REQUEST['password']."'";
	$rs=mysql_query($sql);

	if($row=mysql_fetch_array($rs))
	{
		$message=array(
				'agent_id'=>$row['agent_id'],
				'referral_id'=>$row['referral_id'],
				'company'=>$row['company'],
				'agent_name'=>$row['agent_name'],
				'agent_email'=>$row['agent_email'],
				'password'=>$row['password'],
		);
		return $message;
	}else{
		return array(
				'message'=>'0406'
		);
	}

}

/*修改netcube 密码*/
function updatePassword(){
	$email = $_REQUEST['email'];
	$user = $_REQUEST['user'];
	$newPassword = $_REQUEST['newPassword'];
		//根据user 判断是 customer 还是 agent
		if($user == "agent"){
			$sql="UPDATE `zy_agent_user` SET `password` = '$newPassword' WHERE `agent_email` = '$email'";
		}else{
			$sql="UPDATE zy_user SET `password` = '$newPassword' WHERE email='$email'";
		}
		$rs = mysql_query($sql);
		$row = mysql_fetch_array($rs);
		
		if($row != false){
			return array(
						'message'=>'0409' //成功
				);
		}else{
			return array(
						'message'=>'0501'	//失败
				);
		}
	}
